﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.BLL
{
    public partial class ConsumerAddressBLL : BaseBLL<ConsumerAddressBLL>
    {
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ConsumerAddress model)
        {
            return ConsumerAddressDAL.Instance.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ConsumerAddress model)
        {
            return ConsumerAddressDAL.Instance.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return ConsumerAddressDAL.Instance.Delete(Id);
        }
        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return ConsumerAddressDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ConsumerAddress GetModel(int Id)
        {
            var ds = ConsumerAddressDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<ConsumerAddress>(ds).ToList()[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ConsumerAddress> GetModelList(string strWhere = null)
        {
            var ds = ConsumerAddressDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<ConsumerAddress>(ds).ToList();
        }
        #endregion

        #region 扩展方法

        /// <summary>
        /// 通过用户ID获取地址列表
        /// </summary>
        public List<ConsumerAddress> GetModelListByConsumerId(int consumerId)
        {
            return ConsumerAddressDAL.Instance.GetModelListByConsumerId(consumerId);
        }
        public TableDataViewDto<ConsumerAddress> GetPagingList(int draw, int pageIndex, int pageSize, string searchKeyWord, string colOrder, int consumerId)
        {
            try
            {
                int totalCount = 0;
                List<ConsumerAddress> list = ConsumerAddressDAL.Instance.GetPagingList(pageIndex, pageSize, searchKeyWord, colOrder, ref totalCount, consumerId);
                list.ForEach(a =>
                {
                    a.Action +=
                   "  <a class='btn btn-danger' href='javascript:;' onclick=Edit(" + a.Id + ");>编辑</a>  ";
                    if (a.IsDefault == false)
                    {
                        a.Action +=
                            "<a class='btn btn-warning' href='javascript:;' onclick=SetDefaultAddress(" + a.Id +
                            ");>设定默认地址</a>   ";
                    }
                    //else
                    //{
                    //    a.Action +=
                    //    "<a class='btn btn-success' href='javascript:;'>默认地址</a>   "; 
                    //}
               
                });
                var returnDataList = new TableDataViewDto<ConsumerAddress>
                {
                    data = list,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };
                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }


        public bool DeleteDefaultAddress(int Id)
        {
            return ConsumerAddressDAL.Instance.DeleteDefaultAddress(Id);
        }
        #endregion
    }
}